//
//  LZVideoProgressView.m
//  laziz_Consumer
//
//  Created by xbnzd on 17/3/31.
//  Copyright © 2017年 XBN. All rights reserved.
//

#import "LZVideoProgressView.h"
#import "extobjc.h"

@interface LZVideoProgressView()
@property (weak, nonatomic) IBOutlet UIProgressView *bufferProgressView;   //缓冲进度条
@property (weak, nonatomic) IBOutlet UISlider *playSlider; //播放滑块
@property (weak, nonatomic) IBOutlet UIProgressView *playProgressView; //播放进度

@end

@implementation LZVideoProgressView

- (void)awakeFromNib
{
    [super awakeFromNib];
    
    self.backgroundColor = [UIColor clearColor];

    _bufferProgressView.trackTintColor = [UIColor whiteColor];
    _bufferProgressView.progressTintColor = UIColorFromRGB(0xff7a21, 1);
    _bufferProgressView.layer.cornerRadius = .6;
    _bufferProgressView.layer.masksToBounds = YES;
    _bufferProgressView.progress = 0;
    
    _playSlider.minimumTrackTintColor = [UIColor orangeColor];
    _playSlider.maximumTrackTintColor = [UIColor clearColor];
    _playSlider.value = 0;
    _playSlider.hidden = YES;
    
    _playProgressView.trackTintColor = [UIColor clearColor];
    _playProgressView.progressTintColor = [UIColor greenColor];
    _playProgressView.layer.cornerRadius = .6;
    _playProgressView.layer.masksToBounds = YES;
    _playProgressView.progress = 0;
    _playProgressView.hidden = YES;
}


#pragma mark - set  progress

- (void)resetProgress
{
    if (self.hidden == YES) {
        self.hidden = NO;
    }
//    self.bufferProgressView.progress = 0;
    self.playProgressView.progress = 0;
}
- (void)setBufferProgress:(CGFloat)p
{
    if (self.bufferProgressView.hidden == YES) {
        self.bufferProgressView.hidden = NO;
    }
    [self.bufferProgressView setProgress:p animated:NO];
    if (p > 0.98) {
        @weakify(self);
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            @strongify(self);
            [UIView animateWithDuration:0.3 animations:^{
                self.bufferProgressView.alpha = 0;
            } completion:^(BOOL finished) {
                self.bufferProgressView.hidden = YES;
                self.bufferProgressView.alpha = 1;
            }];
        });
    }
}

- (void)setPlayProgress:(CGFloat)p
{
    if (self.playProgressView.hidden == YES) {
        self.playProgressView.hidden = NO;
    }
    [self.playProgressView setProgress:p animated:NO];
}

- (void)hiddenProgress
{
    self.bufferProgressView.hidden = YES;
    self.playProgressView.hidden = YES;
}
@end
